<template>
	<div id="writeDaily" class="w-h-100" style="box-sizing: border-box; overflow: hidden">
		<div class="w-h-100 ver-flex">
			<el-card class="box-card" shadow="hover" :body-style="{ paddingBottom: '0' }">
				<search v-bind="demandSearchConfig">
					<template #footer>
						<el-button type="primary" icon="ele-Search" @click="query" :style="{ display: $hasPer('demand:query') ? '' : 'none' }"> {{ $t('6byUl8U10pYbuLt9gcer') }} </el-button>
						<el-button icon="ele-Plus" type="primary" @click="addAction" :style="{ display: $hasPer('demand:add') ? '' : 'none' }"> {{ $t('wxVoGkZlNcsigcMzRgP3T') }} </el-button>
					</template>
				</search>
			</el-card>
			<el-card class="w-h-100 flex-1" shadow="hover" :body-style="{ paddingBottom: '0' }" style="margin-top: 8px">
				<inscroll-table :data-list="list" v-bind="demandTableConfig" :dataCount="Number(totalRows)" :handle-size-change="handleSizeChange" :handle-current-change="handleCurrentChange">
					<template #detail="scope">
						<el-button icon="ele-Edit" size="small" text type="primary" @click="() => detailAction(scope.row)" :style="{ display: $hasPer('demand:detail') ? '' : 'none' }">
							{{ $t('tq54cqHgYlTuNmSeqjprg') }}
						</el-button>
					</template>
					<template #change="scope">
						<el-button icon="ele-Files" size="small" text type="primary" @click="() => changeAction(scope.row)" :style="{ display: $hasPer('demand:change') ? '' : 'none' }">
							{{ $t('pjWw7XfwuyAbQfOjK41') }}
						</el-button>
					</template>
					<template #cancel="scope">
						<el-button icon="ele-EditPen" text type="primary" @click="() => deleteAction(scope.row)" :style="{ display: $hasPer('demand:remove') ? '' : 'none' }">
							{{ $t('rftzgN4RveCfaKMhgfK') }}
						</el-button>
					</template>
					<template #checkFlow="scope">
						<div v-if="scope.row && scope.row.checkFlow">{{ scope.row.checkFlow.name }}</div>
					</template>
					<template #state="scope">
						<div v-if="scope.row && scope.row.operationList">
							{{
								scope.row.operationList.state == 1
									? $t('a')
									: scope.row.operationList.state == 2
									? $t('b')
									: scope.row.operationList.state == 3
									? $t('c')
									: scope.row.operationList.state == 4
									? $t('d')
									: scope.row.operationList.state == 5
									? $t('e')
									: ''
							}}
						</div>
					</template>
				</inscroll-table>
			</el-card>
		</div>
		<demand-form ref="formRef" @submit="(param: any) => submitHandle(param)"></demand-form>
	</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import getStaticTableData from '/@/utils/staticGetTableData';
//  获取系统应用列表 ,删除app接口
import { getDemandList, postDemand, postChangeDemand, postDeleteDemand } from '/@/api/platformWork/index';
import { search, demandSearchConfig, inscrollTable, demandTableConfig } from './config/index';
import { ElMessage, ElMessageBox } from 'element-plus';
import demandForm from './componment/demandForm.vue';
import { IPostDemandData } from 'src/types/ys/index';

const formRef = ref();
const { list, totalRows, handleSizeChange, handleCurrentChange, getData } = getStaticTableData(getDemandList, {}, demandSearchConfig.searchObj);

let demandType: any = window.$t('gBqea1uB2BT2cIhGqWba');
let isChangeStatus: Boolean = false; //false 新增, true 修改
let currentSelectData: any = {};
//新增
const addAction = () => {
	ElMessageBox.confirm(window.$t('3u7rItYmPWrqyQncVKd'), window.$t('piOjp1dVaOlnHzZweMZpy'), {
		confirmButtonText: window.$t('pyaUaE7VxocFmcS2hPdpi'),
		cancelButtonText: window.$t('ruIeMgecKpFi5J4EvYhZn'),
		type: 'success',
		center: true,
	})
		.then(() => {
			//需求申请表
			demandType = window.$t('2Qqvt0u4nZjDt3OiclM4v');
			isChangeStatus = false;
			formRef.value.addDemandApplyNew({});
		})
		.catch(() => {
			//数据变更表
			demandType = window.$t('fUhA0FmnnaKmAofkwIp50');
			isChangeStatus = false;
			formRef.value.addChangeDateNew({});
		});
};

//提交表单
const submitHandle = async (param: any) => {

	try {
		if (isChangeStatus) {
			const postData: IPostDemandData = {
				id: currentSelectData.id,
				appDivision: currentSelectData.appDivision,
				applicantName: currentSelectData.applicantName,
				auditProgress: param.auditProgress,
				number: currentSelectData.number,
				demandClass: param.demandClass,
				description: param.description,
				mduleName: param.mduleName,
				needContent: param.needContent,
				poblem: param.poblem,
				pictuctPath: param.pictuctPath[0] ? param.pictuctPath[0].data : '',
				demandType: currentSelectData.demandType,
			};
			const res: any = await postChangeDemand(postData);
			getData();
		} else {
			const postData: IPostDemandData = {
				appDivision: param.appDivision,
				applicantName: param.applicantName,
				auditProgress: param.auditProgress,
				number: '',
				demandClass: param.demandClass,
				description: param.description,
				mduleName: param.mduleName,
				needContent: param.needContent,
				poblem: param.poblem,
				pictuctPath: param.pictuctPath[0] ? param.pictuctPath[0].data : '',
				demandType: demandType,
			};

			const res: any = await postDemand(postData);
			getData({
				pageNo: 1,
				pageSize: 10,
			});
		}
	} catch {}
};
//查看详情操作
const detailAction = (data: any) => {
	if (data.demandType == window.$t('mgTQyT7JJn3BYccxciO84')) {
		formRef.value.detailChangeDate(data);
	} else {
		formRef.value.detailDemandApply(data);
	}
};
//删除操作
const deleteAction = (data: any) => {
	if (data && data.id) {
		ElMessageBox.confirm(window.$t('fyYzuf8Ykk0R2j8N0El11'), window.$t('piOjp1dVaOlnHzZweMZpy'), {
			confirmButtonText: window.$t('aS2TeoRlOl0vyDe85WYcx'),
			cancelButtonText: window.$t('3N3XRcGghvk4bP9pEmeiz'),
			type: 'warning',
			center: true,
		})
			.then(async () => {
				const res: any = await postDeleteDemand({ Id: data.id });
				getData();
			})
			.catch(() => {});
	} else {
		ElMessage.warning(window.$t('6dDOiL6OfZ1MpGdPeF0Pf'));
	}
};
//修改操作
const changeAction = (data: any) => {
	if (data && data.id) {
		isChangeStatus = true;

		if (data.operationList.state == 1 || data.operationList.state == 2) {
			ElMessage({
				message: window.$t('sefwNh6T0y62aIb0nLZvU'),
				type: 'warning',
			});
			return;
		}
		currentSelectData = data;
		if (data.demandType == window.$t('218i7kTxMmsUsizHTnO0M')) {
			formRef.value.addChangeDateNew(data);
		} else {
			formRef.value.addDemandApplyNew(data);
		}
	}
};
//查询操作
const query = (payLoad: any) => {
	getData();
};
</script>
<style lang="scss" scoped>
//vue基本样式
#writeDaily {
	:deep(.el-card__body) {
		height: 100%;
	}
}
</style>
